/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import config.LdapAuth;
import java.util.Collection;
import javax.ejb.EJB;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import models.Messages;
import models.UserRole;
import models.Users;
import services.Impl.MessageServiceImpl;
import services.Impl.UserServiceImpl;
import services.interfaces.IUserService;

/**
 *
 * @author amega
 */
@ManagedBean(name="userBean")
@SessionScoped
public class UserBean {

    /** Creates a new instance of UserBean */
    public UserBean() {
    }
    
    
    @EJB
    private UserServiceImpl userService;
    @EJB
    private MessageServiceImpl messageService;
    private Users curentUser;

    public Users getCurentUser() {
        return curentUser;
    }

    public void setCurentUser(Users curentUser) {
        this.curentUser = curentUser;
    }
    
    private boolean check;

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }
    
    private Long id;
    
    private String login;
    
    private String password;
    
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }
    
    public String regUser() {
       try {
            this.curentUser = userService.getUserByName(login);
            if (this.curentUser != null) {
                this.curentUser = null;
                return "userregerror";
            }
            
        } catch (Exception e) {
             Users user = new Users();
               user.setName(login);
               user.setEmail(email);
               user.setPassword(password);
               user.setRole(UserRole.USER);
               userService.create(user);             
               this.curentUser =  userService.getUserByName(login);
        }
        return  "";
    }
    
    
    
    public String checkUser() {
        
        this.curentUser = userService.getUsersByNameAndPass(login, password);
        if (this.curentUser == null) {     
            return "userloginerror";
        }
       
        return "";
    }
  
    public String logout() {
        this.curentUser = null;
        this.id = null;
        this.login =null;
        this.password = null;
        this.email =null;
        return  "";
    }
    
    public Collection<Users> getAllUsers() {
        return userService.findAll();
    }
    
    public Collection<Messages> getNewUserMessage() {
        
        //return curentUser.getMessagesCollectionInput();
        return messageService.getNewUserMessage(curentUser);
    }
    
      public Collection<Messages> getUserMessage() {
        
        return curentUser.getMessagesCollectionInput();
        //return messageService.getNewUserMessage(curentUser);
    }
      
      public void deleteMess(ActionEvent event){
        Messages mess = (Messages)event.getComponent().getAttributes().get("message");
        curentUser.getMessagesCollectionInput().remove(mess);
    }
      
    public void dM(ActionEvent event) {
        Messages mess = (Messages)event.getComponent().getAttributes().get("message");
        curentUser.getMessagesCollectionInput().remove(mess);
    }  
    
    public void userEdit() {
        userService.edit(curentUser);
        FacesContext.getCurrentInstance().addMessage("userAdd",new FacesMessage("Данные обнавлены"));
    }
}
